<?php


namespace app\model\hotel;

use app\model\BaseModel;
use Carbon\Carbon;
use think\facade\Db;

/**
 * 统计
 * @author Administrator
 *
 */
class Stat extends BaseModel
{
    /**
     * 添加店铺统计(按照天统计)
     * @param array $data
     */
    public function addHotelStat($data)
    {
        $carbon = Carbon::now();
        $condition = [
            'hotel_id' => $data[ 'hotel_id' ],
            'year' => $carbon->year,
            'month' => $carbon->month,
            'day' => $carbon->day
        ];
        $info = model("stat_hotel")->getInfo($condition, 'id');
        if (empty($info)) {
            $stat_data = [
                'hotel_id' => $data[ 'hotel_id' ],
                'year' => $carbon->year,
                'month' => $carbon->month,
                'day' => $carbon->day,
                'day_time' => Carbon::today()->timestamp,
                'create_time' => time()
            ];
            if (isset($data[ 'order_total' ])) {
                $stat_data[ 'order_total' ] = $data[ 'order_total' ];
            }

            if (isset($data[ 'refund_total' ])) {
                $stat_data[ 'refund_total' ] = $data[ 'refund_total' ];
            }
            if (isset($data[ 'order_pay_count' ])) {
                $stat_data[ 'order_pay_count' ] = $data[ 'order_pay_count' ];
            }
            if (isset($data[ 'room_pay_count' ])) {
                $stat_data[ 'room_pay_count' ] = $data[ 'room_pay_count' ];
            }
            if (isset($data[ 'hotel_money' ])) {
                $stat_data[ 'hotel_money' ] = $data[ 'hotel_money' ];
            }
            if (isset($data[ 'platform_money' ])) {
                $stat_data[ 'platform_money' ] = $data[ 'platform_money' ];
            }


            if (isset($data[ 'order_count' ])) {
                $stat_data[ 'order_count' ] = $data[ 'order_count' ];
            }
            if (isset($data[ 'room_count' ])) {
                $stat_data[ 'room_count' ] = $data[ 'room_count' ];
            }

            if (isset($data[ 'visit_count' ])) {
                $stat_data[ 'visit_count' ] = $data[ 'visit_count' ];
            }
            if (isset($data[ 'collect_count' ])) {
                $stat_data[ 'collect_count' ] = $data[ 'collect_count' ];
            }
            $res = model("stat_hotel")->add($stat_data);

        } else {
            $stat_data = [ 'modify_time' => time() ];

            if (isset($data[ 'order_total' ])) {
                $stat_data[ 'order_total' ] = Db::raw('order_total+' . $data[ 'order_total' ]);
            }

            if (isset($data[ 'refund_total' ])) {
                $stat_data[ 'refund_total' ] = Db::raw('refund_total+' . $data[ 'refund_total' ]);
            }
            if (isset($data[ 'order_pay_count' ])) {
                $stat_data[ 'order_pay_count' ] = Db::raw('order_pay_count+' . $data[ 'order_pay_count' ]);
            }
            if (isset($data[ 'room_pay_count' ])) {
                $stat_data[ 'room_pay_count' ] = Db::raw('room_pay_count+' . $data[ 'room_pay_count' ]);
            }
            if (isset($data[ 'hotel_money' ])) {
                $stat_data[ 'hotel_money' ] = Db::raw('hotel_money+' . $data[ 'hotel_money' ]);
            }
            if (isset($data[ 'platform_money' ])) {
                $stat_data[ 'platform_money' ] = Db::raw('platform_money+' . $data[ 'platform_money' ]);
            }

            if (isset($data[ 'order_count' ])) {
                $stat_data[ 'order_count' ] = Db::raw('order_count+' . $data[ 'order_count' ]);
            }
            if (isset($data[ 'room_count' ])) {
                $stat_data[ 'room_count' ] = Db::raw('room_count+' . $data[ 'room_count' ]);
            }

            if (isset($data[ 'visit_count' ])) {
                $stat_data[ 'visit_count' ] = Db::raw('visit_count+' . $data[ 'visit_count' ]);
            }
            if (isset($data[ 'collect_count' ])) {
                $stat_data[ 'collect_count' ] = Db::raw('collect_count+' . $data[ 'collect_count' ]);
            }

            $res = Db::name('stat_hotel')->where($condition)->update($stat_data);
        }
        if ($data[ 'hotel_id' ] != 0) {
            $data[ 'hotel_id' ] = 0;
            $this->addHotelStat($data);
        }
        return $this->success($res);

    }

    /**
     * 获取店铺统计（按照天查询）
     * @param unknown $hotel_id 0表示平台
     * @param unknown $year
     * @param unknown $month
     * @param unknown $day
     */
    public function getStatHotel($hotel_id, $year, $month, $day)
    {
        $condition = [
            'hotel_id' => $hotel_id,
            'year' => $year,
            'month' => $month,
            'day' => $day
        ];
        $info = model("stat_hotel")->getInfo($condition, 'id, hotel_id, year, month, day, order_total, refund_total, order_pay_count, room_pay_count, hotel_money, platform_money, create_time, modify_time, order_count, room_count, day_time');

        if (empty($info)) {
            $condition[ 'day_time' ] = Carbon::today()->timestamp;
            model("stat_hotel")->add($condition);
            $info = model("stat_hotel")->getInfo($condition, 'id, hotel_id, year, month, day, order_total, refund_total, order_pay_count, room_pay_count, hotel_money, platform_money, create_time, modify_time, order_count, room_count, day_time');
        }
        return $this->success($info);
    }

    /**
     * 获取店铺统计信息
     * @param unknown $hotel_id
     * @param unknown $start_time
     */
    public function getHotelStatSum($hotel_id, $start_time = 0)
    {
        $condition = [
            [ 'hotel_id', '=', $hotel_id ]
        ];
        if (!empty($start_time)) {
            $condition[] = [ 'day_time', '>=', $start_time ];
        }
        $info = model("stat_hotel")->getInfo($condition,
            'SUM(order_total) as order_total,SUM(refund_total) as refund_total,SUM(order_pay_count) as order_pay_count,SUM(room_pay_count) as room_pay_count,SUM(hotel_money) as hotel_money,SUM(platform_money) as platform_money,SUM(order_count) as order_count,SUM(room_count) as room_count,');
        if ($info[ 'order_total' ] == null) {
            $info = [
                "order_total" => 0,
                "refund_total" => 0,
                "order_pay_count" => 0,
                "room_pay_count" => 0,
                "hotel_money" => 0,
                "platform_money" => 0,
                "order_count" => 0,
                "room_count" => 0,
            ];
        }
        return $this->success($info);
    }

    /**
     * 获取店铺统计列表
     * @param $hotel_id
     * @param $start_time
     * @return array
     */
    public function getHotelStatList($hotel_id, $start_time)
    {
        $condition = [
            [ 'hotel_id', '=', $hotel_id ],
            [ 'day_time', '>=', $start_time ],
        ];
        $info = model("stat_hotel")->getList($condition,
            'id, hotel_id, year, month, day, order_total, refund_total, order_pay_count, room_pay_count, hotel_money, platform_money, create_time, modify_time, order_count, room_count, day_time');
        return $this->success($info);
    }
}